import requests
# import json
import random
from user_agents import user_agent_list


class TianJi(object):
    """天天基金之基金排行榜数据爬虫
       begin_time, end_time: xxxx-xx-xx;
       start_item, items_per_page: int numbers
    """

    def __init__(self):
        self.start_url = "http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=zzf&st=desc&sd={}&ed={}&qdii=&tabSubtype=,,,,,&pi={}&pn={}&dx=1&v=0.030019517800576434"

        self.headers = {
            "Accept-Language":"zh-CN",
            "Referer": "http://fund.eastmoney.com/data/fundranking.html",
            "User-Agent": random.choice(user_agent_list)
        }
    
    # 构造地址
    def gen_urls(self, *args):
        return self.start_url.format(*args)

    # 发送请求
    def get_content(self, url):
        respons = requests.get(url, headers=self.headers)
        return respons.content.decode()
    
    # 保存数据
    def save_data(self, data):
        with open("test.txt", "a") as f:
            f.write(data)
    
    # 运行蜘蛛
    def run_spider(self):
        begin_time = input("输入要查询的起始时间xxxx-xx-xx：")
        end_time = input("输入要查询的结束时间xxxx-xx-xx：")
        start_item = input("从第几条记录开始查询（正整数）:")
        items_per_page = input("输入要查询的记录条数:")

        url = self.gen_urls(begin_time, end_time, start_item, items_per_page)
        data = self.get_content(url)
        self.save_data(data)

def main():
    tianji = TianJi()
    tianji.run_spider()

if __name__ == '__main__':
    main()

        
